var express = require('express');
var router = express.Router();

let studentsList = [
  {
    id: 1,
    name: "李诚豪"
  },
  {
    id: 2,
    name: "林奥"
  },
  {
    id: 3,
    name: "乌噶"
  },
  {
    id: 4,
    name: "王骏磊"
  },
  {
    id: 5,
    name: "周洪杰"
  },
  {
    id: 6,
    name: "叶家佐"
  },
  {
    id: 7,
    name: "周培钦"
  },
  {
    id: 8,
    name: "卢钰坤"
  },
  {
    id: 9,
    name: "王肖川"
  },
  {
    id: 10,
    name: "徐宁"
  }
]

// 获取列表的接口
router.get('/list', function(req, res, next) {
  // 后端拿前端传过来的参数
  // console.log(req.query);
  const { page, count } = req.query; // 解构的方式拿到数据

  if (!page || !count) {
    res.json({
      success: false,
      message: "参数错误"
    })
  } else {
    const result = studentsList.slice((page - 1) * count, page * count);
    res.json({
      success: true,
      data: result
    })
  }
});


// 删除数据的接口
// post是另一种请求方式
router.post("/del", (req, res) => {
  // console.log(req.query);
  // studentsList = studentsList.filter(item => {
  //   return item.id !== +req.query.id
  // })

  // 如果请求用的是post，后端接受就用body了
  // console.log(req.body);

  if (!req.body.id) {
    res.json({
      success: false,
      message: "参数错误"
    })
  } else {
    studentsList = studentsList.filter(item => {
      return item.id !== +req.body.id
    })
    
    res.json({
      success: true,
      message: "删除成功"
    })
  }
  
})

module.exports = router;
